﻿namespace SLClient.ViewModels
{
    using SLClient.CustomerControls;
    using SLClient.Extension;
    using SLClient.Infrastructure;
    using SLClient.ServiceAPI;
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Data;
    using System.Windows.Input;

    public class GameRecordViewModel : ViewModelBase
    {
        private Visibility _detailesVisibity = Visibility.Collapsed;
        private DateTime _endTime;
        private Visibility _processVisibity = Visibility.Visible;
        private Visibility _reportlistVisibity = Visibility.Visible;
        private SearchUserWindow _searchUserWindow;
        private ViwUserPlayInfo _selectUserPlayInfo;
        private ViwGameReport _selectViwGameReport = new ViwGameReport();
        private DateTime _startTime;

        public GameRecordViewModel()
        {
            this.LowerList = new ObservableCollection<UserInfo>();
            this.UserPlayInfoDetailes = new ObservableCollection<ViwUserPlayInfo>();
            this.ViwReports = new ObservableCollection<ViwGameReport>();
            this.ReportCollection = new PagedCollectionView(this.ViwReports);
            this.UserPlayCollection = new PagedCollectionView(this.UserPlayInfoDetailes);
            this._startTime = DateTime.Now;
            this._endTime = DateTime.Now;
            this.CommandInit();
            this.Init(ViewDispatcher.LoginUser.UserId);
        }

        private void CommandInit()
        {
        }

        public void GetChildLowerList(Guid navguid, ref int postion, ref List<NavUserInfo> list)
        {
            Func<NavUserInfo, bool> func = null;
            if (navguid != Guid.Empty)
            {
                this.GetCurrentLower(navguid);
                postion = 0;
                if (navguid == ViewDispatcher.LoginUser.UserId)
                {
                    list = new List<NavUserInfo>();
                }
                else
                {
                    if (func == null)
                    {
                        func=(s)=>{return s.UserId==navguid;};
                    }
                    NavUserInfo item = Enumerable.First<NavUserInfo>(list, func);
                    int index = list.IndexOf(item);
                    int count = list.Count;
                    list.RemoveRange(index + 1, count - 1);
                }
            }
            else if ((((this._selectViwGameReport.UserId != Guid.Empty) && 
                (this._selectViwGameReport.UserId != ViewDispatcher.LoginUser.UserId)) && 
                (this._selectViwGameReport.UserType == 0)) && (this._selectViwGameReport.Tag != 1))
            {
                this.GetCurrentLower(this._selectViwGameReport.UserId);
                postion = 0;
                NavUserInfo info2 = new NavUserInfo {
                    UserId = this._selectViwGameReport.UserId,
                    UserName = this._selectViwGameReport.UserAccount
                };
                list.Add(info2);
            }
        }

        public void GetCurrentLower(Guid userId)
        {
            ServiceClient instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            instance.GetGameReportByUserIdAndTimeCompleted += delegate (object obj, GetGameReportByUserIdAndTimeCompletedEventArgs arg) {                
                this.ViwReports.Clear();
                for (int j = 0; j < arg.Result.Count; j++)
                {
                    if (j == 0)
                    {
                        arg.Result[j].CanSeeOperation = 0;
                    }
                    else
                    {
                        arg.Result[j].CanSeeOperation = Visibility.Collapsed;
                    }
                    this.ViwReports.Add(arg.Result[j]);
                }
                Guid currentParentGuid = new Guid(arg.UserState.ToString());
                Enumerable.FirstOrDefault<ViwGameReport>(this.ViwReports, 
                   new Func<ViwGameReport, bool>((s) => { return s.UserId == currentParentGuid; })).Tag = 1;                                        
                this.ProcessVisibity = Visibility.Collapsed;
            };
            instance.GetGameReportByUserIdAndTimeAsync(userId, this.StartTime, this.EndTime, userId);
        }

        public void Init(Guid userId)
        {
            ServiceClient instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            instance.GetGameReportByUserIdAndTimeCompleted += delegate (object obj, GetGameReportByUserIdAndTimeCompletedEventArgs arg) {
                this.ViwReports.Clear();
                for (int j = 0; j < arg.Result.Count; j++)
                {
                    if (j == 0)
                    {
                        arg.Result[j].CanSeeOperation = 0;
                    }
                    else
                    {
                        arg.Result[j].CanSeeOperation = Visibility.Collapsed;
                    }
                    this.ViwReports.Add(arg.Result[j]);
                }
                this.ProcessVisibity = Visibility.Collapsed;
                Guid currentParentGuid = new Guid(arg.UserState.ToString());
                Enumerable.FirstOrDefault<ViwGameReport>(this.ViwReports, 
                   new Func<ViwGameReport, bool>((s) => { return s.UserId == currentParentGuid; })).Tag = 1;                         
            };
            instance.GetGameReportByUserIdAndTimeAsync(userId, this.StartTime, this.EndTime, userId);
        }

        private static Func<NavUserInfo, bool> myfunc;
        public void InitDetailes(ref int pos1, ref List<NavUserInfo> list)
        {
            pos1 = 0;
            this.ReportlistVisibity = Visibility.Collapsed;
            this.DetailesVisibity = 0;
            this.ProcessVisibity = 0;
            ServiceClient instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            instance.GetGameReportDetailesByUserIdAndTimeCompleted += delegate (object obj, GetGameReportDetailesByUserIdAndTimeCompletedEventArgs arg) {
                this.UserPlayInfoDetailes.Clear();
                foreach (ViwUserPlayInfo info in arg.Result)
                {
                    this.UserPlayInfoDetailes.Add(info);
                }
                this.ProcessVisibity = Visibility.Collapsed;
            };
            instance.GetGameReportDetailesByUserIdAndTimeAsync(this.SelectViwGameReport.UserId, this.StartTime, this.EndTime);
            if (myfunc == null)
            {
                // return ((i.UserName == "盈亏明细") && (i.UserId == Guid.Empty));

                myfunc = new Func<NavUserInfo, bool>((s) => { return ((s.UserName == "盈亏明细") && (s.UserId == Guid.Empty));});
            }
            if (Enumerable.Count<NavUserInfo>(list, myfunc) == 0)
            {
                NavUserInfo item = new NavUserInfo {
                    UserId = Guid.Empty,
                    UserName = "明细记录"
                };
                list.Add(item);
            }
        }

        public void InvokeUserPlayDeatiles(Guid userId)
        {
            ServiceClient instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            instance.GetGameReportDetailesByUserIdAndTimeCompleted += delegate (object obj, GetGameReportDetailesByUserIdAndTimeCompletedEventArgs arg) {
                this.UserPlayInfoDetailes.Clear();
                foreach (ViwUserPlayInfo info in arg.Result)
                {
                    this.UserPlayInfoDetailes.Add(info);
                }
                this.ProcessVisibity = Visibility.Collapsed;
            };
            instance.GetGameReportDetailesByUserIdAndTimeAsync(userId, this.StartTime, this.EndTime);
        }

        private static Func<NavUserInfo, bool> myfunc1;
        public void NavClick(ref List<NavUserInfo> list)
        {
            if (this.DetailesVisibity == Visibility.Visible)
            {
                this.DetailesVisibity = Visibility.Collapsed;
                this.ReportlistVisibity = Visibility.Visible;
                if (myfunc1 == null)
                {
                    myfunc1 = new Func<NavUserInfo, bool>((s)=>{return ((s.UserName == "明细记录") && (s.UserId == Guid.Empty));});
                }
                if (Enumerable.Count<NavUserInfo>(list, myfunc1) > 0)
                {
                    list.RemoveAt(list.Count - 1);
                }
            }
        }

        public void RefreshDetailList(ref int postion, ref List<NavUserInfo> list)
        {
            Guid userId;
            this.ProcessVisibity = 0;
            postion = 0;
            if (list.Count == 1)
            {
                userId = ViewDispatcher.LoginUser.UserId;
            }
            else
            {
                userId = list[list.Count - 2].UserId;
            }
            this.InvokeUserPlayDeatiles(userId);
        }

        public void RefreshList(ref int position, ref List<NavUserInfo> list)
        {
            Guid userId;
            position = 0;
            this.ProcessVisibity = 0;
            if (list.Count > 0)
            {
                userId = list[list.Count - 1].UserId;
            }
            else
            {
                userId = ViewDispatcher.LoginUser.UserId;
            }
            this.Init(userId);
        }

        public void Search(ref int pos, ref int pos1, ref List<NavUserInfo> list)
        {
            Guid userId;
            pos = pos1 = 0;
            this.ProcessVisibity = 0;
            if (this.ReportlistVisibity == 0)
            {
                if (list.Count > 0)
                {
                    userId = list[list.Count - 1].UserId;
                }
                else
                {
                    userId = ViewDispatcher.LoginUser.UserId;
                }
                this.Init(userId);
            }
            else
            {
                if (list.Count == 1)
                {
                    userId = ViewDispatcher.LoginUser.UserId;
                }
                else
                {
                    userId = list[list.Count - 2].UserId;
                }
                this.InvokeUserPlayDeatiles(userId);
            }
        }

        public void SearchList(ref int pos)
        {
            this._searchUserWindow = new SearchUserWindow();
            pos = 0;
            this._searchUserWindow.SearchHander += new EventHandler(this.SearchUserWindowSearchHander);
            this._searchUserWindow.Show();
        }

        private void SearchUserWindowSearchHander(object sender, EventArgs e)
        {
            PagedCollectionView reportCollection = this.ReportCollection;
            reportCollection.Filter = (Predicate<object>) Delegate.Combine(reportCollection.Filter, 
                 new Func<ViwGameReport, bool>((s) => { return s.UserAccount.Contains(this._searchUserWindow.tbUserName.Text);}));
        }

        public void ShowOrderDetail()
        {
            new OrderWindow(this.SelectUserPlayInfo).Show();
        }

        private static Func<NavUserInfo, bool> myfunc2;
        public void Transition(ref List<NavUserInfo> list)
        {
            if (this._reportlistVisibity == Visibility.Visible)
            {
                this.ReportlistVisibity = Visibility.Collapsed;
                this.DetailesVisibity = Visibility.Visible;
                if (myfunc2 == null)
                {
                    myfunc2 = new Func<NavUserInfo, bool>((s)=>{return ((s.UserName == "明细记录") && (s.UserId == Guid.Empty));});
                }
                if (Enumerable.Count<NavUserInfo>(list, myfunc2) == 0)
                {
                    NavUserInfo item = new NavUserInfo {
                        UserId = Guid.Empty,
                        UserName = "明细记录"
                    };
                    list.Add(item);
                }
            }
            else
            {
                this.ReportlistVisibity = Visibility.Visible;
                this.DetailesVisibity = Visibility.Collapsed;
                if (myfunc2 == null)
                {
                    myfunc2 = new Func<NavUserInfo, bool>((s) => { return ((s.UserName == "明细记录") && (s.UserId == Guid.Empty)); });
                }
                if (Enumerable.Count<NavUserInfo>(list, myfunc2) > 0)
                {
                    list.RemoveAt(list.Count - 1);
                }
            }
        }

        public Visibility DetailesVisibity
        {
            get
            {
                return this._detailesVisibity;
            }
            set
            {
                this._detailesVisibity = value;
                base.Notify<Visibility>(() => this.DetailesVisibity);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(GameRecordViewModel)), (MethodInfo)(GameRecordViewModel.get_DetailesVisibity)), new ParameterExpression[0]));
            }
        }

        public DateTime EndTime
        {
            get
            {
                return this._endTime;
            }
            set
            {
                this._endTime = value;
                base.Notify<DateTime>(() => this.EndTime);
                //base.Notify<DateTime>(System.Linq.Expressions.Expression.Lambda<Func<DateTime>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(GameRecordViewModel)), (MethodInfo)(GameRecordViewModel.get_EndTime)), new ParameterExpression[0]));
            }
        }

        public ObservableCollection<UserInfo> LowerList { get; set; }

        public Visibility ProcessVisibity
        {
            get
            {
                return this._processVisibity;
            }
            set
            {
                this._processVisibity = value;
                base.Notify<Visibility>(() => this.ProcessVisibity);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(GameRecordViewModel)), (MethodInfo)(GameRecordViewModel.get_ProcessVisibity)), new ParameterExpression[0]));
            }
        }

        public PagedCollectionView ReportCollection { get; set; }

        public Visibility ReportlistVisibity
        {
            get
            {
                return this._reportlistVisibity;
            }
            set
            {
                this._reportlistVisibity = value;
                base.Notify<Visibility>(() => this.ReportlistVisibity);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(GameRecordViewModel)), (MethodInfo)(GameRecordViewModel.get_ReportlistVisibity)), new ParameterExpression[0]));
            }
        }

        public ICommand SearchCommand { get; set; }

        public ICommand SearchListCommand { get; set; }

        public ViwUserPlayInfo SelectUserPlayInfo
        {
            get
            {
                return this._selectUserPlayInfo;
            }
            set
            {
                this._selectUserPlayInfo = value;
                base.Notify<ViwUserPlayInfo>(() => this.SelectUserPlayInfo);
                //base.Notify<ViwUserPlayInfo>(System.Linq.Expressions.Expression.Lambda<Func<ViwUserPlayInfo>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(GameRecordViewModel)), (MethodInfo)(GameRecordViewModel.get_SelectUserPlayInfo)), new ParameterExpression[0]));
            }
        }

        public ViwGameReport SelectViwGameReport
        {
            get
            {
                return this._selectViwGameReport;
            }
            set
            {
                this._selectViwGameReport = value;
                base.Notify<ViwGameReport>(() => this.SelectViwGameReport);
                //base.Notify<ViwGameReport>(System.Linq.Expressions.Expression.Lambda<Func<ViwGameReport>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(GameRecordViewModel)), (MethodInfo)(GameRecordViewModel.get_SelectViwGameReport)), new ParameterExpression[0]));
            }
        }

        public DateTime StartTime
        {
            get
            {
                return this._startTime;
            }
            set
            {
                this._startTime = value;
                base.Notify<DateTime>(() => this.StartTime);
                //base.Notify<DateTime>(System.Linq.Expressions.Expression.Lambda<Func<DateTime>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(GameRecordViewModel)), (MethodInfo)(GameRecordViewModel.get_StartTime)), new ParameterExpression[0]));
            }
        }

        public ICommand TransitionCommand { get; set; }

        public PagedCollectionView UserPlayCollection { get; set; }

        private ObservableCollection<ViwUserPlayInfo> UserPlayInfoDetailes { get; set; }

        private ObservableCollection<ViwGameReport> ViwReports { get; set; }
    }
}

